package com.base.api.cdr.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.base.api.cdr.PersonalQueryService;
import com.base.dao.cdr.PersonalVisitMapper;
import com.base.utils.TreeUtil;
import com.base.vo.cdr.PersonalVisitDetail;
import com.base.vo.cdr.PersonalVisitInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author wnhuang
 * @Package com.base.api.cdr.impl
 * @date 2020-12-20 20:57
 */
@Service
public class PersonalQueryServiceImpl implements PersonalQueryService {

    @Autowired
    PersonalVisitMapper personalVisitMapper;

    /**
     * 查询患者所有就诊记录
     *
     * @param map
     * @return
     */
    @Override
    public String getPersonalAll(Map<String, Object> map) {
        Map<String, Object> map1 = new HashMap<>();
        try {
            List<PersonalVisitInfo> list = personalVisitMapper.queryAllPersonalVisit(map);
            map1.put("Data", TreeUtil.listToTree(JSONArray.parseArray(JSONObject.toJSONString(toTree(list))), "label", "pid", "children"));
            map1.put("code", "0");
        } catch (Exception e) {
            map1.put("code", "error");
            map1.put("msg", e.getMessage());
        }
        return JSONObject.toJSONString(map1);
    }

    private List<Map<String, Object>> toTree(List<PersonalVisitInfo> list) {
        List<Map<String, Object>> list2 = new ArrayList<>();
        Set<Object> set = new HashSet<>();
        for (PersonalVisitInfo personalVisitInfo : list) {
            Map<String, Object> map2 = new HashMap<>();
            Date dxdate = personalVisitInfo.getDxDate();
            String yyyy = new SimpleDateFormat("YYYY").format(dxdate);
            set.add(yyyy);
            map2.put("label", new SimpleDateFormat("MM-dd").format(dxdate) + " " + personalVisitInfo.getOpOrIp() + " " + personalVisitInfo.getDxDoctorName() + " " + personalVisitInfo.getDepartment());
            map2.put("pid", yyyy);
            map2.put("data", personalVisitInfo);
            map2.put("id", UUID.randomUUID());
            List<PersonalVisitDetail> children = personalVisitInfo.getChildren();
            for (PersonalVisitDetail personalVisitDetail : children) {
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(personalVisitDetail));
                jsonObject.put("label", jsonObject.get("name"));
                jsonObject.put("pid", new SimpleDateFormat("MM-dd").format(dxdate) + " " + personalVisitInfo.getOpOrIp() + " " + personalVisitInfo.getDxDoctorName() + " " + personalVisitInfo.getDepartment());
                jsonObject.put("id", UUID.randomUUID());
                jsonObject.put("visitNumber", personalVisitInfo.getVisitNumber());
                list2.add(jsonObject);
            }
            list2.add(map2);
        }
        Iterator<Object> iterator = set.iterator();
        Map<String, Object> map1 = new HashMap<>();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            map1.put("label", next);
            map1.put("pid", "0");
            map1.put("id", UUID.randomUUID());
        }
        list2.add(map1);
        return list2;
    }


}
